const db = wx.cloud.database();
const _ = db.command;

/**
 * 获取试题列表
 * @param {Object} params 查询参数
 * @returns {Promise} 返回试题列表
 */
async function getQuestionList(params = {}) {
  try {
    const { subject, type, difficulty, keyword, page = 1, size = 10 } = params;
    
    // 构建查询条件
    const query = {};
    if (subject) query.subject = subject;
    if (type) query.type = type;
    if (difficulty) query.difficulty = difficulty;
    if (keyword) {
      query.title = db.RegExp({
        regexp: keyword,
        options: 'i'
      });
    }
    
    // 获取总数
    const countResult = await db.collection('questions').where(query).count();
    const total = countResult.total;
    
    // 获取列表数据
    const listResult = await db.collection('questions')
      .where(query)
      .skip((page - 1) * size)
      .limit(size)
      .orderBy('createTime', 'desc')
      .get();
    
    return {
      success: true,
      data: {
        list: listResult.data,
        total
      }
    };
  } catch (error) {
    console.error('获取试题列表失败', error);
    return {
      success: false,
      message: '获取试题列表失败'
    };
  }
}

/**
 * 获取试题详情
 * @param {string} questionId 试题ID
 * @returns {Promise} 返回试题详情
 */
async function getQuestionDetail(questionId) {
  try {
    const result = await db.collection('questions').doc(questionId).get();
    return {
      success: true,
      data: result.data
    };
  } catch (error) {
    console.error('获取试题详情失败', error);
    return {
      success: false,
      message: '获取试题详情失败'
    };
  }
}

/**
 * 创建试题
 * @param {Object} question 试题数据
 * @returns {Promise} 返回创建结果
 */
async function createQuestion(question) {
  try {
    const now = new Date();
    const data = {
      ...question,
      createTime: now,
      updateTime: now,
      status: 'normal'
    };
    
    const result = await db.collection('questions').add({ data });
    return {
      success: true,
      data: {
        _id: result._id
      }
    };
  } catch (error) {
    console.error('创建试题失败', error);
    return {
      success: false,
      message: '创建试题失败'
    };
  }
}

/**
 * 更新试题
 * @param {string} questionId 试题ID
 * @param {Object} data 更新数据
 * @returns {Promise} 返回更新结果
 */
async function updateQuestion(questionId, data) {
  try {
    const now = new Date();
    const updateData = {
      ...data,
      updateTime: now
    };
    
    await db.collection('questions').doc(questionId).update({
      data: updateData
    });
    
    return {
      success: true
    };
  } catch (error) {
    console.error('更新试题失败', error);
    return {
      success: false,
      message: '更新试题失败'
    };
  }
}

/**
 * 删除试题
 * @param {string} questionId 试题ID
 * @returns {Promise} 返回删除结果
 */
async function deleteQuestion(questionId) {
  try {
    await db.collection('questions').doc(questionId).remove();
    return {
      success: true
    };
  } catch (error) {
    console.error('删除试题失败', error);
    return {
      success: false,
      message: '删除试题失败'
    };
  }
}

/**
 * 批量导入试题
 * @param {Array} questions 试题列表
 * @returns {Promise} 返回导入结果
 */
async function batchImportQuestions(questions) {
  try {
    const now = new Date();
    const data = questions.map(question => ({
      ...question,
      createTime: now,
      updateTime: now,
      status: 'normal'
    }));
    
    const result = await db.collection('questions').add({
      data
    });
    
    return {
      success: true,
      data: {
        count: questions.length
      }
    };
  } catch (error) {
    console.error('批量导入试题失败', error);
    return {
      success: false,
      message: '批量导入试题失败'
    };
  }
}

module.exports = {
  getQuestionList,
  getQuestionDetail,
  createQuestion,
  updateQuestion,
  deleteQuestion,
  batchImportQuestions
}; 